﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.DAL;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using OrganizedIN.BAL.Common;

namespace OrganizedIN.UI.Photos
{
    public partial class add_new_photos : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        string user;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                user = Convert.ToString(Session["UserID"]);

                lblUserID.Text = Convert.ToString(Session["UserID"]);
                if (!IsPostBack)
                {
                    //get photo album
                    ds = Common.getPhotoAlbum(user);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            ListItem li = new ListItem(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
                            drpAlbum.Items.Add(li);
                        }
                    }
                }

                if (IsPostBack && drpAlbum.SelectedItem.ToString() == "+Create Album")
                {
                    pnlAddAlbum.Visible = true;
                }
                else
                {
                    pnlAddAlbum.Visible = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSavePhoto_Click(object sender, EventArgs e)
        {
            /* @Author: Shrikant Meena
             * @Purpose: Upload Photo
             * @Date: 14 April 2011
             */
            string strFilename = string.Empty, strExt = string.Empty;
            string strAppPath = Request.PhysicalApplicationPath;
            string strUploadPath = strAppPath + "\\UploadedImages\\";
            string strFinalRelPath = string.Empty;
            try
            {
                strFilename = txtFileUpload.FileName.ToString();
                strExt = System.IO.Path.GetExtension(strFilename);
                if (strExt == ".jpg" || strExt == ".png" || strExt == ".jpeg")
                {
                    string imgFileName = "USER_IMAGE_" + Convert.ToString(Session["UserID"]) + DateTime.Now.ToString("hhmmss") + strExt;
                    string strFinalImagePath = strUploadPath + imgFileName;
                    strFinalRelPath = "../../UploadedImages/" + imgFileName;

                    // Actually upload the original file to server
                    txtFileUpload.PostedFile.SaveAs(strFinalImagePath);

                    // Writing the final data to the database
                    Common.Add_New_Photo(lblUserID.Text, txtTitle.Text, strFinalRelPath, txtSummary.Text, drpRating.SelectedValue.ToString(), "A", txtEventName.Text, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), txtShare.Text, drpAlbum.SelectedValue);

                    // Add the photo to the album
                    // Commom.Add_Photo_Album(params)

                    // Display the status of the actions we performed
                    lblStatus.ForeColor = Color.Green;
                    lblStatus.Text = "Photo uploaded successfully!";
                }
                else
                {
                    lblStatus.ForeColor = Color.Red;
                    lblStatus.Text = "Only upload jpg, jpeg and png kind of files";
                }

                txtTitle.Text = "";
                txtEventName.Text = "";
                txtShare.Text = "";
                txtSummary.Text = "";
                txtTitle.Text = "";
                drpAlbum.SelectedValue = "";
                drpRating.SelectedValue = "5";

            }
            catch (Exception ex)
            {
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = "Some error occured during upload";
            }
        }

        protected void btnCancelAddAlbum_Click(object sender, EventArgs e)
        {
            pnlAddAlbum.Visible = false;
            drpAlbum.SelectedIndex = 0;
        }

        protected void btnCreateAlbum_Click(object sender, EventArgs e)
        {
            try
            {
                ds = Common.getPhotoAlbumName(user, txtNewAlbum.Text);
                if (ds.Tables[0].Rows[0][0].ToString() == "0")
                {
                    ds = Common.addPhotoAlbum(user, txtNewAlbum.Text);
                    lblalbummsg.ForeColor = Color.Green;
                    lblalbummsg.Text = "Album successfully created";
                    drpAlbum.SelectedIndex = 0;
                    pnlAddAlbum.Visible = false;
                    ds = Common.getPhotoAlbum(user, txtNewAlbum.Text);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            ListItem li = new ListItem(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
                            drpAlbum.Items.Add(li);
                        }
                    }
                }
                else
                {
                    lblalbummsg.Text = "Album already exist";
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "There is some error";
            }
        }
    }
}
